package com.yonyou.cyxdms.basedata.service.controller.series;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.basedata.service.dto.CarserialDTO;
import com.yonyou.cyxdms.basedata.service.dto.SeriesDTO;
import com.yonyou.cyxdms.basedata.service.entity.series.SeriesPO;
import com.yonyou.cyxdms.basedata.service.service.series.CarserialService;
import com.yonyou.cyxdms.basedata.service.vo.CarserialDetailsVO;
import com.yonyou.cyxdms.basedata.service.vo.CarserialInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * CarserialController
 *
 * @author zhangsx
 * @since 2018-09-08
 */
@RestController
@RequestMapping("/seriesModel")
@Api(tags = {"车系相关接口"})
public class CarserialController extends BaseController {
    final Logger logger = LoggerFactory.getLogger(CarserialController.class);

    @Autowired
    public CarserialService carSerialPoService;

    /**
     * 分页查询数据
     *
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author zhangsx
     * @since 2018-09-08
     */
    @ResponseBody
    @GetMapping("/")
    @SuppressWarnings("unchecked")
    public IPage<Map<String, Object>> getList(@RequestParam Map<String, Object> queryCondition) {
        Page<Map<String, Object>> page = new Page(1, 2);
        return carSerialPoService.selectPageBysql(page, queryCondition);
    }

    /**
     * 进行数据修改
     *
     * @param id 数据主键ID
     * @return Map<String                                                               ,                                                               Object> 数据对应的map 结构
     * @return Map<String   ,       Object> 数据对应的map 结构
     * @author zhangsx
     * @since 2018-09-08
     */
    @ResponseBody
    @GetMapping(value = "/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        return carSerialPoService.getById(id);
    }

    /**
     * series进行数据新增
     *
     * @param seriesDTO 需要保存的DTO
     * @return Map<String                               ,                               Object> 返回的数据map  "seriesId":26,"brandId":58,"seriesCode":"58we","seriesName":"rtew","isValid":1,"isImport":1,"sourceChangedAt":"1536831038000","bizId":"add"}
     * @return Map<String   ,       Object> 返回的数据map  "seriesId":26,"brandId":58,"seriesCode":"58we","seriesName":"rtew","isValid":1,"isImport":1,"sourceChangedAt":"1536831038000","bizId":"add"}
     * @author zhangsx
     * @since 2018-09-08
     */
    @ResponseBody
    @PostMapping(value = "/series")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> insert(@RequestBody SeriesDTO seriesDTO) {
        logger.info("seriesDTO{}", seriesDTO);
        Map<String, Object> map = null;
        CarserialDTO carserialDTO = new CarserialDTO();
        if ("add".equals(seriesDTO.getBizId())) {
            //新增
            carserialDTO.setBrandId(seriesDTO.getBrandId());
            carserialDTO.setSourceId(seriesDTO.getSourceId());
            carserialDTO.setSeriesCode(seriesDTO.getSeriesCode());
            carserialDTO.setIsImport(seriesDTO.getIsImport());
            carserialDTO.setIsValid(seriesDTO.getIsValid());
            carserialDTO.setSourceChannel(seriesDTO.getSourceChannel());
            carserialDTO.setAppId("ff7555e8ac239560");
            carserialDTO.setName(seriesDTO.getSeriesName());
            map = carSerialPoService.insert(carserialDTO);
        } else if ("update".equals(seriesDTO.getBizId())) {
            //更新
            carserialDTO.setBrandId(seriesDTO.getBrandId());
            carserialDTO.setSourceId(seriesDTO.getSourceId());
            carserialDTO.setSeriesCode(seriesDTO.getSeriesCode());
            carserialDTO.setIsImport(seriesDTO.getIsImport());
            carserialDTO.setIsValid(seriesDTO.getIsValid());
            carserialDTO.setSourceChannel(seriesDTO.getSourceChannel());
            carserialDTO.setAppId("ff7555e8ac239560");
            carserialDTO.setName(seriesDTO.getSeriesName());
            //根据传递的条件进行查询更新
            LambdaQueryWrapper<SeriesPO> queryWrapper = new QueryWrapper<SeriesPO>().lambda();
            queryWrapper.eq(SeriesPO::getSourceId, seriesDTO.getSourceId());
            SeriesPO carSerialPo = carSerialPoService.getOne(queryWrapper);
            logger.debug("CarserialId:{}", carSerialPo.getSeriesId());
            SeriesPO po = new SeriesPO();
            BeanUtils.copyProperties(seriesDTO, po);
            map = carSerialPoService.update(carSerialPo.getSeriesId(), carserialDTO);
        }
        return map;
    }

    /**
     * 进行数据修改
     *
     * @param id           需要修改数据的ID
     * @param carserialDTO 需要保存的DTO
     * @return Map<String                                                               ,                                                               Object> 返回的数据map
     * @return Map<String   ,       Object> 返回的数据map
     * @author zhangsx
     * @since 2018-09-08
     */
    @ResponseBody
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(@PathVariable("id") Long id, @RequestBody CarserialDTO carserialDTO) {
        return carSerialPoService.update(id, carserialDTO);
    }

    /**
     * 查询list
     *
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map                               <                               java.lang.String                               ,                               java.lang.Object>>
     * @return java.util.List<java.util.Map       <       java.lang.String   ,       java.lang.Object>>
     * @author guorp1
     * @since 2018/9/29
     */
    @ResponseBody
    @GetMapping("/selectListBySql")
    public List<Map<String, Object>> selectListBySql(@RequestParam Map<String, Object> queryCondition) {
        return carSerialPoService.selectListBySql(queryCondition);
    }

    /**
     * 根据车系Id获取车系图片开始
     *
     * @param serialId
     * @return java.lang.String
     * @author Administrator
     * @since 2018/10/8
     */
    @ResponseBody
    @GetMapping("/logoUrl")
    public String logoUrl(@RequestParam Long serialId) {
        return carSerialPoService.logoUrl(serialId);
    }

    /**
     * 按条件查询品牌车系信息
     *
     * @param queryCondition 查询条件     ,                               java.lang.Object>
     * @return java.util.Map<java.lang.String   ,       java.lang.Object>
     * @author renwd
     * @since 2018/11/26
     */
    @GetMapping("/querySerialByCondition")
    public Map<String, Object> querySerialByCondition(@RequestParam Map<String, Object> queryCondition) {
        return carSerialPoService.querySerialByCondition(queryCondition);
    }


    /**
     * 根据品牌ID查询车系列表信息
     *
     * @author dengqiang
     * @since 2019/1/2
     */

    @GetMapping(value = "/carserialList", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(notes = "车系列表信息", value = "车系列表信息")
    @ApiImplicitParam(name = "brandId", value = "品牌ID", required = true)
    public List<CarserialInfoVO> queryCarserialInfo(@RequestParam(required = false) String brandId,
                                                    @RequestParam(required = false) String ownerCode,
                                                    @RequestHeader("appId") String appId) {
        logger.info("brandId:{}", brandId);
        return carSerialPoService.queryCarserialInfo(brandId, appId, ownerCode);
    }

    /**
     * 根据车系ID查询车系详情
     *
     * @author dengqiang
     * @since 2019/1/2
     */
    @GetMapping(value = "/queryCarserial", produces = {"application/json;" +
            "charset=UTF-8"})
    @ApiImplicitParam(name = "carserialId", value = "车系ID", required = true)
    @ApiOperation(notes = "车系详情", value = "车系详情")
    public CarserialDetailsVO queryCarserialDetails(@RequestParam(value = "carserialId") Long carserialId,
                                                    @RequestHeader("appId") String appId) {
        return carSerialPoService.queryCarserialDetails(carserialId, appId);
    }


}